/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Welcome;

import RMS.LoadImageInfoFromRecord;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class DrawChoiceActivated extends Canvas {

    private Image bufferImage;
    private int PAD_BT = 5;
    private int PAD_LR = 10;
    private int choiceWidth, choiceHeight = 30;
    private int getLine = 1;
    private Start midlet;
    private Graphics g;
    private int startScroll, endScroll;
    private Font font;
    private int padding_top, logoHeight, unlockWidth;
    private int instanceKeyCode, trans;
    ReDownloadImage rdi;
    public static int time = 10;
    LoadImageInfoFromRecord lifr;

    public DrawChoiceActivated(Start midlet) {
        this.setFullScreenMode(true);
        System.out.println("jshfdjkhfjks");
        bufferImage = Image.createImage(getWidth(), getHeight());
        choiceWidth = getWidth() - 2 * PAD_LR;
        font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        padding_top = (choiceHeight - font.getHeight()) / 2;
        startScroll = 0;
        endScroll = DrawChoice.logo.getHeight() + 4 + 3 * choiceHeight + 2 * PAD_BT;
        g = bufferImage.getGraphics();
        DrawChoice.demBai = 0;
        this.midlet = midlet;
    }

    public void paint(Graphics g) {
        if (lifr.getRecordCount() == 30) {
            renderTheWorld();
        } else {
            rdi = new ReDownloadImage(midlet);
            midlet.getDis().setCurrent(rdi);
        }
        g.drawImage(bufferImage, 0, 0, Graphics.LEFT | Graphics.TOP);
    }

    public void renderTheWorld() {

        g.setColor(0x00f0dfb6);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(DrawChoice.logo, getWidth() / 2, 0, Graphics.HCENTER | Graphics.TOP);

        logoHeight = DrawChoice.logo.getHeight();
        unlockWidth = DrawChoice.unlock.getWidth();

        g.setColor(0x00ffffff);
        g.drawRoundRect(PAD_LR - 1, logoHeight + 4 - 1 + trans, choiceWidth + 1, choiceHeight + 1, 5, 5);
        g.drawRoundRect(PAD_LR - 1, logoHeight + 4 + choiceHeight + PAD_BT - 1 + trans, choiceWidth + 1, choiceHeight + 1, 5, 5);
        g.drawRoundRect(PAD_LR - 1, logoHeight + 4 + 2 * (choiceHeight + PAD_BT) - 1 + trans, choiceWidth + 1, choiceHeight + 1, 5, 5);

        g.setColor(0x00b0711f);
        if (getLine == 1) {
            g.setColor(0x00E99649);
        }
        g.fillRoundRect(PAD_LR, DrawChoice.logo.getHeight() + 4 + trans, choiceWidth, choiceHeight, 5, 5);

        g.setColor(0x00b0711f);
        if (getLine == 2) {
            g.setColor(0x00E99649);
        }
        g.fillRoundRect(PAD_LR, logoHeight + 4 + choiceHeight + PAD_BT + trans, choiceWidth, choiceHeight, 5, 5);

        g.setColor(0x00b0711f);
        if (getLine == 3) {
            g.setColor(0x00E99649);
        }
        g.fillRoundRect(PAD_LR, logoHeight + 4 + 2 * (choiceHeight + PAD_BT) + trans, choiceWidth, choiceHeight, 5, 5);

        g.setFont(font);

        g.setColor(0x002C2929);
        if (getLine == 1) {
            g.setColor(0x00ffffff);
        }
        g.drawString("Khởi động", PAD_LR + choiceWidth / 2, logoHeight + 4 + padding_top + trans,
                Graphics.HCENTER | Graphics.TOP);
        g.drawImage(DrawChoice.unlock, PAD_LR + 5 + unlockWidth / 2, logoHeight + 4 + choiceHeight / 2 + trans,
                Graphics.HCENTER | Graphics.VCENTER);

        g.setColor(0x002C2929);
        if (getLine == 2) {
            g.setColor(0x00ffffff);
        }
        g.drawString("Tăng tốc", PAD_LR + choiceWidth / 2, logoHeight + 4 + choiceHeight + padding_top + PAD_BT + trans,
                Graphics.HCENTER | Graphics.TOP);
        g.drawImage(DrawChoice.unlock, PAD_LR + 5 + unlockWidth / 2, logoHeight + 4 + 3 * choiceHeight / 2 + PAD_BT + trans,
                Graphics.HCENTER | Graphics.VCENTER);

        g.setColor(0x002C2929);
        if (getLine == 3) {
            g.setColor(0x00ffffff);
        }
        g.drawString("Về đích", PAD_LR + choiceWidth / 2, logoHeight + 4 + 2 * choiceHeight + padding_top + 2 * PAD_BT + trans,
                Graphics.HCENTER | Graphics.TOP);
        g.drawImage(DrawChoice.unlock, PAD_LR + 5 + unlockWidth / 2, logoHeight + 4 + 5 * choiceHeight / 2 + 2 * PAD_BT + trans,
                Graphics.HCENTER | Graphics.VCENTER);

        g.setColor(0x00b0711f);
        font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(font);
        g.fillRect(0, getHeight() - font.getHeight() - 4, getWidth(), font.getHeight() + 4);
        g.setColor(0x00ffffff);
        g.drawLine(0, getHeight() - font.getHeight() - 4, getWidth(), getHeight() - font.getHeight() - 4);
        g.drawString("Trở lại", 1, getHeight() - 2, Graphics.LEFT | Graphics.BOTTOM);
    }

    protected void pointerPressed(int x, int y) {
        int temp;
        if (x >= PAD_LR - 1 && x <= PAD_LR + choiceWidth) {
            temp = logoHeight + 4 + trans;
            if (y >= temp - 1 && y <= temp + choiceHeight) {
                if (DrawChoice.khoiDong) {
                    DrawChoice.tangToc = DrawChoice.tt = false;
                    DrawChoice.veDich = DrawChoice.vd = false;
                    midlet.disManager.emptyStack();
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(new KhoiDong(midlet, 60));
                } else {
                    DrawChoice.kd = true;
                    midlet.disManager.emptyStack();
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(new DownloadAlert2(midlet));
                }
            } else {

                temp = logoHeight + 4 + trans + choiceHeight + PAD_BT;
                if (y >= temp - 1 && y <= temp + choiceHeight) {
                    if (DrawChoice.tangToc) {
                        DrawChoice.khoiDong = DrawChoice.kd = false;
                        DrawChoice.veDich = DrawChoice.vd = false;
                        midlet.disManager.emptyStack();
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new TangToc(midlet, 50));
                    } else {
                        DrawChoice.tt = true;
                        midlet.disManager.emptyStack();
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new DownloadAlert2(midlet));
                    }
                } else {

                    temp = logoHeight + 4 + 2 * (choiceHeight + PAD_BT) + trans;
                    if (y >= temp - 1 && y <= temp + choiceHeight) {
                        if (DrawChoice.veDich) {
                            DrawChoice.khoiDong = DrawChoice.kd = false;
                            DrawChoice.tangToc = DrawChoice.tt = false;
                            midlet.disManager.emptyStack();
                            midlet.disManager.pushDisplayable(this);
                            midlet.disManager.pushDisplayable(new LevelActivated(midlet, 40));
                        } else {
                            DrawChoice.vd = true;
                            midlet.disManager.emptyStack();
                            midlet.disManager.pushDisplayable(this);
                            midlet.disManager.pushDisplayable(new DownloadAlert2(midlet));
                        }
                    }
                }
            }
            if (x >= 0 && x <= font.stringWidth("Dung choi") + 10) {
                if (y <= getHeight() && y >= getHeight() - font.getHeight() - 10) {
                    midlet.disManager.home();
                }
            }
        }
    }

    protected void keyPressed(int keyCode) {
        instanceKeyCode = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);

        switch (instanceKeyCode) {
            case KeyCodeAdapter.KEY_2:
            case KeyCodeAdapter.UP_KEY:
                getLine--;
                if (getLine == 0) {
                    getLine = 1;
                }
                if (startScroll < 0) {
                    endScroll += 5;
                    startScroll += 5;
                    trans += 5;
                }

                repaint(PAD_LR - 1, logoHeight + 4 - 1, choiceWidth + 2,
                        getHeight() - logoHeight - 3 - font.getHeight() - 4);

                break;

            case KeyCodeAdapter.KEY_8:
            case KeyCodeAdapter.DOWN_KEY:
                getLine++;
                if (getLine == 4) {
                    getLine = 3;
                }
                if (endScroll > (getHeight() - 2 * font.getHeight() - 12)) {
                    endScroll -= 5;
                    startScroll -= 5;
                    trans -= 5;
                }

                repaint(PAD_LR - 1, logoHeight + 4 - 1, choiceWidth + 2,
                        getHeight() - logoHeight - 3 - font.getHeight() - 4);

                break;

            case KeyCodeAdapter.KEY_5:
            case KeyCodeAdapter.CENTER_KEY:
                trans = 0;
                startScroll = 0;
                endScroll = DrawChoice.logo.getHeight() + 4 + 3 * choiceHeight + 2 * PAD_BT;

                if (getLine == 1) {
                    if (DrawChoice.khoiDong) {
                        DrawChoice.tangToc = DrawChoice.tt = false;
                        DrawChoice.veDich = DrawChoice.vd = false;
                        midlet.disManager.emptyStack();
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new KhoiDong(midlet, 60));
                    } else {
                        DrawChoice.kd = true;
                        DrawChoice.vd = DrawChoice.tt = false;
                        midlet.disManager.emptyStack();
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new DownloadAlert2(midlet));
                    }
                } else if (getLine == 2) {
                    if (DrawChoice.tangToc) {
                        DrawChoice.khoiDong = DrawChoice.kd = false;
                        DrawChoice.veDich = DrawChoice.vd = false;
                        midlet.disManager.emptyStack();
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new TangToc(midlet, 50));
                    } else {
                        DrawChoice.tt = true;
                        DrawChoice.kd = DrawChoice.vd = false;
                        midlet.disManager.emptyStack();
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new DownloadAlert2(midlet));
                    }
                } else if (getLine == 3) {
                    if (DrawChoice.veDich) {
                        DrawChoice.khoiDong = DrawChoice.kd = false;
                        DrawChoice.tangToc = DrawChoice.tt = false;
                        midlet.disManager.emptyStack();
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new LevelActivated(midlet, 40));
                    } else {
                        DrawChoice.vd = true;
                        DrawChoice.tt = DrawChoice.kd = false;
                        midlet.disManager.emptyStack();
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new DownloadAlert2(midlet));
                    }
                }
                break;

            case KeyCodeAdapter.SOFT_KEY_LEFT:
                midlet.disManager.home();
                break;
        }
    }

    public int getChoiceHeight() {
        return choiceHeight;
    }

    public int getChoiceWidth() {
        return choiceWidth;
    }
}
